precision highp float;

attribute vec4 a_Position;
attribute vec2 a_TexCoord;    
attribute vec2 a_SideCoord;    

uniform mat4 u_ModelMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ProjMatrix;
uniform vec2 u_Resolution;
uniform vec2 u_Size;
uniform vec2 u_CurrentAngle;
uniform float u_SizeAttenuation;
uniform vec2 u_Offset;
uniform vec3 u_Move;
uniform float u_IsShow;

varying vec2 v_TexCoord; 
varying float v_Dis; 
varying float v_IsShow;

void main() {
    v_IsShow = u_IsShow;
    if (u_IsShow == 0.0) {
        gl_Position = vec4(0.0, 0.0, 0.0, 0.0);
    }
    else {
        vec3 positionNow = a_Position.xyz - u_Move.xyz;
        vec4 finalPosition = u_ProjMatrix * u_ViewMatrix * u_ModelMatrix * vec4(positionNow, a_Position.w);

        float aspect = u_Resolution.x / u_Resolution.y;
        float pixelWidthRatio = 1.0 / (u_Resolution.x * u_ProjMatrix[0][0]);
        float pixelWidth = finalPosition.w * pixelWidthRatio;
        float w = 2.3 * pixelWidth * u_Size.x / aspect;
        float h = 2.3 * pixelWidth * u_Size.y;
        float posiOffsetX = 2.3 * pixelWidth * u_Offset.x * 2.0 / aspect;
        float posiOffsetY = 2.3 * pixelWidth * u_Offset.y * 2.0;
        if( u_SizeAttenuation == 1.0 ) {
            w = 2.3 * u_Size.x / aspect;
            h = 2.3 * u_Size.y;
            posiOffsetX = 2.3 * pixelWidth * u_Offset.x * 2.0 / aspect;
            posiOffsetY = 2.3 * pixelWidth * u_Offset.y * 2.0;
        }
        vec2 offset = vec2(a_SideCoord.x * w - posiOffsetX, a_SideCoord.y * h - posiOffsetY);
        finalPosition.xy += offset.xy;

        gl_Position = finalPosition;

        // 计算该点与球面最靠近摄像机点z之差，距离太远则丢弃像素
        v_Dis = -u_ViewMatrix[3][2] - gl_Position.w;
        v_TexCoord = a_TexCoord;   
    }
}
